<template>
  <view>
    <!-- 搜索框 -->
    <view class="home-search">
      <my-search></my-search>
    </view>
    <!-- 轮播图-->
    <swiper indicator-dots autoplay :interval="3000" class="swiper" circular>
      <swiper-item v-for="(item,i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航栏-->
    <view class="nav-list" v-for="(item,i) in navList" :key="i" @click="goToCate(item)">
      <image :src="item.image_src" class="nav-image"></image>
    </view>
    <!-- 楼层标题-->
    <view class="floor-list">
      <view class="floor-item" v-for="(item,i) in floorList" :key="i">
        <!-- 楼层标题-->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <view class="floor-img-box">
          <!-- 左侧-->
          <navigator class="left-img-box" :url="item.product_list[0].navigator_url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <!-- 右侧-->
          <view class="right-img-box">
            <navigator class="right-img-item" v-for="(value,index) in item.product_list" :key="index" v-if="index !==0"
              :url="value.navigator_url">
              <image :src="value.image_src" :style="{width: value.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badge from '@/mixins/tabbar-badge.js'
  export default {
    data() {
      return {
        swiperList: [], //轮播图数组,
        navList: [], //导航数组
        floorList: [] //楼层数组
      }
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    mixins:[badge],
    methods: {
      // 获取轮播图数据
      async getSwiperList() {
        //获取里面的data 赋值给 res
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        // console.log(res.data.message)
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.swiperList = res.message
      },
      //获取导航数据
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.navList = res.message
      },
      //进行跳转页面
      goToCate(item) {
        if (item.name === "分类") {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      //获取楼层导航数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        //对获取到的数据中的navigator_url进行改造
        // 原来的navigator_url的值是 /pages/goods_list?query = 服饰 , 改造后是 query = 服饰
        //先在获取到的数据中的每一个进行遍历,获取里面的product_list, floor_title不变
        this.floorList = res.message.map(v => {
          return {
            //在对获取到的product_list数组进行遍历,获取里面的navigator_url,其他的不变
            product_list: v.product_list.map(value => {
              return {
                ...value,
                //对navigator_url的数据以 ? 分开成一个新数组获取数组中的第二个值
                navigator_url: '/subpkg/goods_list/goods_list?' + value.navigator_url.split('?')[1]
              }
            }),
            floor_title: v.floor_title
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .floor-img-box {
    display: flex;
    margin-left: 10rpx;

    .right-img-box {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
  }

  .swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    margin-top: 30rpx;
    display: inline-block;
    width: 25%;
    height: 140rpx;

    .nav-image {
      margin: 20rpx;
      width: 128rpx;
      height: 100%;
    }
  }

  .floor-title {
    height: 60rpx;
    width: 100%;
    display: flex;
  }

  .home-search {
    border-top: 0.5rpx solid red;
    z-index: 99;
    position: sticky;
    top: 0;
    left: 0;
    right: 0;
  }
</style>
